package haoyou.csnf.manager.module.pm.controller.admin.positionsseq;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.positionsseq.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.positionsseq.PositionsSeqDO;
import haoyou.csnf.manager.module.pm.service.positionsseq.PositionsSeqService;

@Tag(name = "管理后台 - 职位序列")
@RestController
@RequestMapping("/pm/positions-seq")
@Validated
public class PositionsSeqController {

    @Resource
    private PositionsSeqService positionsSeqService;

    @PostMapping("/create")
    @Operation(summary = "创建职位序列")
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:create')")
    public CommonResult<Long> createPositionsSeq(@Valid @RequestBody PositionsSeqSaveReqVO createReqVO) {
        return success(positionsSeqService.createPositionsSeq(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位序列")
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:update')")
    public CommonResult<Boolean> updatePositionsSeq(@Valid @RequestBody PositionsSeqSaveReqVO updateReqVO) {
        positionsSeqService.updatePositionsSeq(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位序列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:delete')")
    public CommonResult<Boolean> deletePositionsSeq(@RequestParam("id") Long id) {
        positionsSeqService.deletePositionsSeq(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职位序列")
                @PreAuthorize("@ss.hasPermission('pm:positions-seq:delete')")
    public CommonResult<Boolean> deletePositionsSeqList(@RequestParam("ids") List<Long> ids) {
        positionsSeqService.deletePositionsSeqListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位序列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:query')")
    public CommonResult<PositionsSeqRespVO> getPositionsSeq(@RequestParam("id") Long id) {
        PositionsSeqDO positionsSeq = positionsSeqService.getPositionsSeq(id);
        return success(BeanUtils.toBean(positionsSeq, PositionsSeqRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位序列分页")
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:query')")
    public CommonResult<PageResult<PositionsSeqRespVO>> getPositionsSeqPage(@Valid PositionsSeqPageReqVO pageReqVO) {
        PageResult<PositionsSeqDO> pageResult = positionsSeqService.getPositionsSeqPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PositionsSeqRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位序列 Excel")
    @PreAuthorize("@ss.hasPermission('pm:positions-seq:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPositionsSeqExcel(@Valid PositionsSeqPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PositionsSeqDO> list = positionsSeqService.getPositionsSeqPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位序列.xls", "数据", PositionsSeqRespVO.class,
                        BeanUtils.toBean(list, PositionsSeqRespVO.class));
    }

}